APP.directive('goTop', ['$rootScope', '$state', '$ionicScrollDelegate', '$timeout',
  function ($rootScope, $state, $ionicScrollDelegate, $timeout) {
    return {
      restrict: 'AE',
      scope: false,
      replace: true,
      template: '<i class="icon ion-android-arrow-dropup-circle font-size-icon right-20 bottom-20 assertive fixed front" ng-show="showGoTop"></i>',
      link: function (scope, element, attr) {

        var SCROLL_HEIGHT = 100;//滚动多少距离后出现GOtop按钮。
        var control = undefined;
        var scrollElement = undefined;
        var operated = false;
        scope.showGoTop = false;
        $timeout(function () {
          console.log(scope);
          control = scope.$$childHead.scrollCtrl;
          scrollElement = control.element;
        }, 100);

        var listener = function () {
          if (!!control && !!control.getScrollPosition() && !operated) {
            var top = control.getScrollPosition().top;
            scope.showGoTop = top > SCROLL_HEIGHT;
            operated = true;
            $timeout(function () {
              operated = false;
            }, 500)
          }
        };
        $timeout(function () {
          ionic.on('scroll', listener, scrollElement);
        }, 2000);

        element.on('click', function () {
          control.scrollTop(true);//滚动到顶部
          $timeout(function () {
            scope.showGoTop = false;
          }, 200);
        });
      }
    }
  }]);
